﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.ArticleManage.Controls
{
	public partial class ModuleArticle : BaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				this.DataBind();
			}
		}

		protected override void OnInit(EventArgs e)
		{
			CBLChannelIds.DataBind();
			
			base.OnInit(e);
		}

		public override int? SiteId
		{
			get
			{
				if(base.SiteId.HasValue)
					return base.SiteId;
				return Modules.GetItem(int.Parse(Request.QueryString["ModuleID"])).SiteId;
			}
		}

		/// <summary>
		/// 属性 ArticleOrder
		/// </summary>
		public int ArticleOrder
		{
			get
			{
				return Convert.ToInt32(CtrlArticleOrder.Text);
			}
			set
			{
				CtrlArticleOrder.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 ReleaseDate
		/// </summary>
		public DateTime ReleaseDate
		{
			get
			{
				return Convert.ToDateTime(CtrlReleaseDate.Text);
			}
			set
			{
				CtrlReleaseDate.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 IsEffective
		/// </summary>
		public bool IsEffective
		{
			get
			{
				return Convert.ToBoolean(CtrlIsEffective.Text);
			}
			set
			{
				CtrlIsEffective.Text = value.ToString();
			}
		}
		public string ChannelIds
		{
			get
			{
				return StringExtensions.CasingCompound<string>(CBLChannelIds.Values, Companies.OperationTypeSeparator);
			}
			set
			{
				if (!string.IsNullOrEmpty(value))
					CBLChannelIds.Values = StringExtensions.CasingDecompound<string>(value, Companies.OperationTypeSeparator);
			}
		}	

		public string RegionIds
		{
			get
			{				
				List<string> values = new List<string>();
				foreach (ListItem item in CBLRegionIds.Items)
					if (item.Selected) values.Add(item.Value);
				return StringExtensions.CasingCompound<string>(values, Companies.OperationTypeSeparator);
			}
			set
			{
				if (!string.IsNullOrEmpty(value))
				{
					CBLRegionIds.DataBind();
					var values = StringExtensions.CasingDecompound<string>(value, Companies.OperationTypeSeparator);
					foreach (ListItem item in CBLRegionIds.Items)
						item.Selected = values.Contains(item.Value);
				}
			}
		}

		public bool IsRecommend
		{
			get
			{
				return Convert.ToBoolean(CtrIsRecommend.Text);
			}
			set
			{
				CtrIsRecommend.Text = value.ToString();
			}
		}



		/// <summary>
		/// 控件的 ModuleArticleInfo 实例。
		/// </summary>
		public Module.ModuleArticle Info
		{
			get
			{
				Module.ModuleArticle moduleArticle = new Module.ModuleArticle();

				moduleArticle.ArticleOrder = ArticleOrder;
				moduleArticle.IsEffective = IsEffective;
				moduleArticle.ReleaseDate = ReleaseDate;
				moduleArticle.ChannelIds = string.IsNullOrEmpty(ChannelIds) ? "" : ChannelIds;
				moduleArticle.IsRecommend = IsRecommend;
				moduleArticle.RegionIds = RegionIds;

				return moduleArticle;
			}
			set
			{
				if (value == null) return;

				ArticleOrder = value.ArticleOrder;
				IsEffective = value.IsEffective;
				ReleaseDate = value.ReleaseDate;
				IsRecommend = value.IsRecommend;
				ChannelIds = value.ChannelIds;
				RegionIds = value.RegionIds;
			}
		}

		private bool isBindCtrList = false;
		protected void CBLRegionIds_DataBinding(object sender, EventArgs e)
		{
			if (isBindCtrList) return;
			CBLRegionIds.DataSource = RegionBlacks.GetBaseList(SiteId, null, null, "", null);
			CBLRegionIds.DataTextField = "RegionName";
			CBLRegionIds.DataValueField = "RegionId";
		}

		protected void CBLRegionIds_DataBound(object sender, EventArgs e)
		{
			CBLRegionIds.DataSource = null;
			isBindCtrList = true;
		}
	}
}